package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.AppHomeBean;
import top.drinyeit.weilong.model.SecCategoryBean;
import top.drinyeit.weilong.model.SecondCategoryBean;

/**
 * Created by wu on 2018/1/25.
 */

public class SecondCategoryAdapter extends RecyclerView.Adapter<SecondCategoryAdapter.ViewHolder> {
    Context context;
    private List<SecCategoryBean> data;

    public SecondCategoryAdapter(List<SecCategoryBean> data) {
        this.data = data;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_text,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int realPosition = position -1;
        if (position == 0){
            holder.text.setText("全部商品");
        }else {
            holder.text.setText(data.get(realPosition).getCategory_name());
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView text;
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            text = itemView.findViewById(R.id.tv_text);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
